題目
Sock Merchant
舉例輸入
9
10 20 20 10 10 30 50 10 20
舉例輸出
3
解析
每個數字代表一隻襪子
判斷陣列中能湊出幾雙襪子
function sockMerchant(n, ar) {
let result = 0;
let sockType = {};
//先計算所有襪子類型的數量
ar.forEach(el => {
let typeCount = sockType[el] || 0;
sockType[el] = typeCount + 1;
});
//襪子類型數量可以湊出幾雙
for (var i in sockType) {
result = result + Math.floor((sockType[i] / 2))
}
return result
}